#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo
const int ledPin =  13;      // the number of the LED pin
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
int previousVal = 90;
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

int pos = 0;    // variable to store the servo position
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}
 
void loop()
{
  servoControl();
  ledBlink();
}

void servoControl() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  //val = (val + previousVal) / 2;
  if (val != previousVal) {
    myservo.write(val);
    delay(30); 
    previousVal = val;
    Serial.println(val);
  }

}

void ledBlink() {
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}
